package com.ruoyi.shoufang.controller;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.shoufang.domain.AjaxResult;
import com.ruoyi.shoufang.domain.TSfContract;
import com.ruoyi.shoufang.service.TSfContractService;
import com.ruoyi.system.api.model.LoginUser;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.agenda.DefaultActivitiEngineAgenda;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("activiti")
public class ActivitiController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private TSfContractService sfContractService;

    //部署流程
    @GetMapping("deployment")
    public void deployment() {
        repositoryService.createDeployment()
                .name("合同审批流程")
                .addClasspathResource("diagram.png")
                .addClasspathResource("Examined.bpmn20.xml")
                .deploy();
    }

    //启动流程
    @GetMapping("start")
    public void start(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user1", "admin");
        map.put("user2", "zhangsi");
        map.put("user3", "zhangwu");
        TSfContract sfContract = sfContractService.getById(id);
        sfContract.setReserve2(0);
        runtimeService.startProcessInstanceByKey("Examined", sfContract.getId().toString(), map);
        sfContractService.updateFlagById(sfContract);
        execution(id);
    }

    //
    @GetMapping("execution")
    public void execution(Integer id) {
        LoginUser loginUser = SecurityContextHolder.get(SecurityConstants.LOGIN_USER, LoginUser.class);
        TSfContract sfContract = sfContractService.getById(id);
        ProcessInstance instance = runtimeService.createProcessInstanceQuery()
                .processInstanceBusinessKey(sfContract.getId().toString())
                .singleResult();
        if (loginUser.getUsername().equals("president")) {
            sfContract.setReserve2(1);
            sfContractService.updateFlagById(sfContract);
        }
        Task task = taskService.createTaskQuery()
                .processInstanceId(instance.getProcessInstanceId())
                .taskAssignee(loginUser.getUsername())
                .singleResult();
        Map<String, Object> map = new HashMap<>();
        taskService.complete(task.getId(), map);
    }



    //审核列表
    @GetMapping("noFinished")
    public AjaxResult noFinished() {
        List<TSfContract> result = new ArrayList<>();
        LoginUser loginUser = SecurityContextHolder.get(SecurityConstants.LOGIN_USER, LoginUser.class);
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee(loginUser.getUsername())
                .list();
        for (HistoricTaskInstance historicTaskInstance : list) {
            String processInstanceId = historicTaskInstance.getProcessInstanceId();
            HistoricProcessInstance processInstances = historyService.createHistoricProcessInstanceQuery()
                    .processInstanceId(processInstanceId).singleResult();
            String businessKey = processInstances.getBusinessKey();
            TSfContract contractById = sfContractService.getById(Integer.valueOf(businessKey));
            contractById.setReserve3(loginUser.getUsername());
            result.add(contractById);
        }
        return AjaxResult.success(result);
    }

    //代办任务
    @GetMapping("unFinished")
    public AjaxResult unFinished(){
        List<TSfContract> result = new ArrayList<>();
        LoginUser loginUser = SecurityContextHolder.get(SecurityConstants.LOGIN_USER, LoginUser.class);
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee(loginUser.getUsername())
                .list();
        for (Task task : list) {
            ProcessInstance instance = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(task.getProcessInstanceId())
                    .singleResult();
            String businessKey = instance.getBusinessKey();
            TSfContract contractById = sfContractService.getById(Integer.valueOf(businessKey));
            contractById.setReserve3(loginUser.getUsername());
            result.add(contractById);
        }
        return AjaxResult.success(result);
    }

}
